package com.example.demo.service.impl;

import com.example.demo.mapper.UsersMapper;
import com.example.demo.pojo.Users;
import com.example.demo.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName UserServiceImpl
 * @Description TODO
 * @Author zhangshuai
 * @Date 2019/6/30 15:31
 * @Version 1.0
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper usersMapper;


    @Override
    public Users selectUser(Long id) {
        return usersMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saves() {

        Users users = new Users();
        for (int i = 0; i < 50; i++) {
            users.setUserName("xiaoming" + i);
            users.setPassword("123" + i);
            users.setNickName("mingming" + i);
            users.setUserSex("0" + i);
            usersMapper.insert(users);
        }

    }

    @Override
    public Map<String, Object> selectUser() {
        Map<String, Object> map = new HashMap<>();

        Page<Object> page = PageHelper.startPage(2, 3);
        List<Users> usersList = usersMapper.selectUser();
        PageInfo<Users> pageInfo = new PageInfo<>(usersList);

        map.put("usersList", usersList);
        //map.put("usersListString", usersList.toString());
        map.put("total", page.getTotal());
        map.put("pageNumber", page.getPageNum());
        map.put("pageSize", page.getPageSize());
        //map.put("pageInfo", pageInfo);

        return map;
    }
}
